package com.example.bluetoothttss;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class BtAdapter extends BaseAdapter {

    private Context context;

    private List<BluetoothDevice> list;

    public BtAdapter(Context context, List<BluetoothDevice> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();//存放控件
            //布局加载器：导入布局
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_bt, null, false);
            holder.tvName = convertView.findViewById(R.id.tvName);//控件存入，方便滑动复用
            convertView.setTag(holder);//防止数据错乱
        } else {
            holder = (Holder) convertView.getTag();//复用
        }

        BluetoothDevice device = list.get(position);

        if (device.getName() == null) {
            holder.tvName.setText(device.getAddress());
        } else {
            holder.tvName.setText(device.getName() + ":" + device.getAddress());
        }
        return convertView;
    }
    class Holder {
        TextView tvName;
    }
}
